package com.hilda.twitter.samples.timeline.ui.adapters;

import twitter4j.ResponseList;
import twitter4j.Status;
import twitter4j.User;
import android.content.Context;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.imageloader.ImageLoader;
import com.google.android.imageloader.ImageLoader.BindResult;
import com.google.android.imageloader.ImageLoader.Callback;
import com.hilda.twitter.samples.timeline.R;
import com.hilda.twitter.samples.timeline.utils.Log;

public class TimelineAdapter extends BaseAdapter implements Callback {

	private static final int LOAD_IMAGE = 0;
	private static final int LOADED_IMAGE = 1;
	
	private ResponseList<Status> mList;
	private LayoutInflater mInflater;
	private ImageLoader mImageLoader;
	private Animation mFadeInAnimation;
	
	public TimelineAdapter(Context context, ResponseList<Status> list) {
		mList = list;
		mInflater = LayoutInflater.from(context);
		mImageLoader = ImageLoader.get(context);
		mFadeInAnimation = AnimationUtils
				.loadAnimation(context, R.anim.fade_in);
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return ((Status) mList.get(position)).getId();
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		ViewHolder holder;

		Status status = mList.get(position);
		User user = status.getUser();
		String url = user.getProfileImageURL().toString();

		if (view == null) {
			view = mInflater.inflate(R.layout.status, null);

			holder = new ViewHolder();

			holder.text = (TextView) view.findViewById(R.id.statusText);
			holder.author = (TextView) view.findViewById(R.id.statusAuthor);
			holder.time = (TextView) view.findViewById(R.id.statusTime);
			holder.avatar = (ImageView) view.findViewById(R.id.statusAvatar);

			// holder.avatar.setAnimation(mFadeInAnimation);

			mImageLoader.prefetch(url);

			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		
		holder.author.setText(user.getScreenName());
		holder.text.setText(status.getText());

		long time = status.getCreatedAt().getTime();

		/*
		 * http://stackoverflow.com/questions/3910042/javaandroid-convert-sqlite-
		 * date-to-x-days-ago
		 */
		String timeDiff = DateUtils.getRelativeTimeSpanString(time,
				System.currentTimeMillis(), 0, DateUtils.FORMAT_ABBREV_ALL)
				.toString();

		holder.time.setText(timeDiff);

		// Log.d("getView: ", url);

		BindResult result = mImageLoader.bind(holder.avatar, url, this);
		// TODO: Test doing thing depending on the value of result (loading a
		// stub image when loading...)
		
		if (result == BindResult.LOADING) {
			holder.avatar.setTag(LOAD_IMAGE);
		}

		return view;
	}

	static class ViewHolder {
		TextView author;
		TextView text;
		TextView time;
		ImageView avatar;
	}

	@Override
	public void onImageLoaded(ImageView view, String url) {
		int value;
		Object tag = view.getTag();
		
		if (tag != null){
			value = (Integer) tag;
			if(value == LOAD_IMAGE){
				view.startAnimation(mFadeInAnimation);
				view.setTag(LOADED_IMAGE);
			}
		}		
	}

	@Override
	public void onImageError(ImageView view, String url, Throwable error) {
		// TODO loading an error image instead of the avatar
		Log.e("onImageError: " + url, error);
	}
}
